package src.com.java.dtgh.oneDay;

import src.com.java.Util;

public class demo2 {

    public static void main(String[] args) {

        int[][] arr = {{0,0,0},
        {0,1,0},
        {0,0,0}};

        demo2 demo2 = new demo2();
        demo2.uniquePathsWithObstacles(arr);
    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if(obstacleGrid[0][0] == 1){
            return 0;
        }
        int[][] arr = new int[obstacleGrid.length][obstacleGrid[0].length];
        solve(obstacleGrid, arr);
//        Util.printArray2(obstacleGrid);
//        System.out.println();
//        Util.printArray2(arr);
        return arr[obstacleGrid.length - 1][obstacleGrid[0].length - 1] ;

    }

    public void solve(int[][] obstacleGrid,int[][] dp) {

        boolean iFlag = true;
        boolean jFlag = true;

        for(int i=0;i<obstacleGrid.length;i++) {
            for(int j=0;j<obstacleGrid[0].length;j++) {
                if(i == 0 && j == 0){
                    dp[i][j] = 1;
                    continue;
                }
                int x = obstacleGrid[i][j];
                if(i == 0 && x == 1){
                    iFlag = false;
                }
                if(j == 0 && x == 1){
                    jFlag = false;
                }

                if(i == 0){
                    if(iFlag){
                        dp[i][j] = 1;
                    }else {
                        dp[i][j] = 0;
                    }
                    continue;
                }
                if(j == 0){
                    if(jFlag){
                        dp[i][j] = 1;
                    }else {
                        dp[i][j] = 0;
                    }
                    continue;
                }

                if(x == 1){
                    dp[i][j] = 0;
                }else {
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }

            }
        }

    }
}
